<?php

	class FtpManager {

		private $_config = array();
		private $_connectionId = false;
		private $_systemType = '';

		function __construct( $config ) {
			$this->_config[ 'host' ] = isset( $config[ 'host' ] ) ? $config[ 'host' ] : 'localhost';
			$this->_config[ 'user' ] = isset( $config[ 'user' ] ) ? $config[ 'user' ] : '';
			$this->_config[ 'password' ] = isset( $config[ 'password' ] ) ? $config[ 'password' ] : '';
			$this->_config[ 'passive' ] = isset( $config[ 'passive' ] ) ? $config[ 'password' ] : true;
			$this->connect( $this->_config[ 'host' ], $this->_config[ 'user' ], $this->_config[ 'password' ], $this->_config[ 'passive' ] );
		}

		function __destruct() {
			if( $this->_connectionId !== false ) {
				@ftp_close( $this->_connectionId );
			}
		}

		public function connect( $host = 'localhost', $user = '', $password = '', $passive = true ) {
			if( $connectionId = @ftp_connect( $host ) ) {
				if( $loginResult = @ftp_login( $connectionId, $user, $password ) ) {
					$this->_connectionId = $connectionId;
					$this->_systemType = ftp_systype ( $this->_connectionId );
					if( $passive === true ) {
						@ftp_pasv( $this->_connectionId, true ); 
					}
					return true;
				}
				return false;
			} else {
				return false;
			}
		}

		public function upload( $source, $destination ) {
			return @ftp_put( $this->_connectionId, $destination, $source, FTP_BINARY );
		}

		public function download( $source, $destination = false ) {
			if( $destination === false ) {
				$destination = tempnam( sys_get_temp_dir(), 'FTP' );
			}
			if( @ftp_get( $this->_connectionId, $destination, $source, FTP_BINARY ) ) {
				return $destination;
			}
			return false;
		}

		public function listDir( $directory = '.', $recurrsive = false ) {
			$list = @ftp_rawlist( $this->_connectionId, $directory );
			$result = array( 'files' => array(), 'folders' => array() );
			foreach( $list as $row ) {
				if( substr( $row, 0, 1 ) == 'd' ) {
					$result[ 'folders' ][] = array(
						'name' => substr( $row, 52 )
					);
				} else {
					$size = explode( ' ', trim( substr( $row, 25 ) ), 2 );
					$result[ 'files' ][] = array(
						'name' => substr( $row, 52 ),
						'size' => $size[ 0 ]
					);
				}
			}
			if( $recurrsive === true ) {
				foreach( $result[ 'folders' ] as $i => $folder ) {
					$result[ 'folders' ][ $i ][ 'list' ] = $this->listDir( $directory . '/' . $folder[ 'name' ] );
				}
			}
			return $result;
		}

		public function getCurrentDirectory() {
			return ftp_pwd( $this->_connectionId );
		}

		public function changeDirectory( $directory ) {
			return ftp_chdir( $this->_connectionId, $directory );
		}

		public function cdDotDot( $directory ) {
			return ftp_cdup( $this->_connectionId );
		}

		public function chmod( $permission, $file ) {
			return ftp_chmod( $this->_connectionId, $permission, $file );
		}

		public function delete( $file ) {
			return ftp_delete( $this->_connectionId, $file );
		}

		public function makeDirectory( $dir ) {
			return ftp_mkdir( $this->_connectionId, $dir );
		}

		public function removeDirectory( $dir ) {
			return ftp_rmdir( $this->_connectionId, $dir );
		}

		public function rename( $old, $new ) {
			return ftp_rename( $this->_connectionId, $old, $new );
		}

		public function move( $old, $new ) {
			return $this->rename( $old, $new );
		}

		public function getSystemType() {
			return $this->_systemType;
		}
	}